package com.sinog.front.controller.biz.common;

import com.alibaba.fastjson.JSONArray;
import com.sinog.core.model.UserDTO;
import com.sinog.core.utilbean.IdGen;
import com.sinog.core.utilbean.Result;
import com.sinog.front.feign.biz.AzbjDatabaseInfoService;
import com.sinog.front.feign.biz.GovernmentHelpService;
import com.sinog.front.feign.biz.PlacementBaseInfoService;
import com.sinog.front.feign.biz.SocialEmploymentService;
import com.sinog.front.feign.biz.SocialOrganizeInfoService;
import com.sinog.front.feign.biz.SocialTrainingService;
import com.sinog.front.feign.biz.StaffInfoManageService;
import com.sinog.front.feign.biz.VolunteerManageService;
import com.sinog.front.service.azbj.uploadappendix.UploadExcelService;
import com.sinog.front.service.user.UserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @desc 安帮工作资源管理 列表导出公用类
 * @author lzq
 * @date 2021-04-29 17:02
 */
@RestController
public class CommonExportController {

    @Autowired
    private UploadExcelService uploadExcelService;
    @Autowired
    private UserFacade userService;
    @Resource
    private StaffInfoManageService staffInfoManageService;
    @Resource
    private PlacementBaseInfoService placementBaseInfoService;
    @Resource
    private SocialTrainingService socialTrainingService;
    @Resource
    private SocialEmploymentService socialEmploymentService;
    @Resource
    private GovernmentHelpService governmentHelpService;
    @Resource
    private SocialOrganizeInfoService socialOrganizeInfoService;
    @Autowired
    private VolunteerManageService volunteerManageService;
    @Autowired
    private AzbjDatabaseInfoService azbjDatabaseInfoService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 工作台
     */
    private static final String WORK_WORDER_INFO = "0";
    /**
     * 安帮工作人员信息管理
     */
    private static final String AB_WORDER_INFO = "1";
    /**
     * 安置帮教基地信息管理
     */
    private static final String AZBJ_JD_INFO = "2";
    /**
     * 就业培训机构管理
     */
    private static final String JY_PEIXUN_INFO = "3";
    /**
     * 就业岗位信息管理
     */
    private static final String JY_POSITION_INFO = "4";
    /**
     * 政府救助帮扶信息管理
     */
    private static final String ZF_JZBF_INFO = "5";
    /**
     * 安帮社会组织信息管理
     */
    private static final String AB_SHZZ_INFO = "6";
    /**
     * 志愿者信息管理
     */
    private static final String ZYZ_INFO = "7";
    /**
     * 帮教资料
     */
    private static final String BJ_ZL = "8";
    /**
     * 批量没勾选
     */
    private static final String PL_MGX = "9";

    /**
     * 工作台数据批量导出
     * @param request request
     * @param response response
     * @param maps maps
     * @return Result
     */
    @PostMapping("/azbj/abwordresource/common/exportExcelFile")
    public Result exportExcelFile(HttpServletRequest request,HttpServletResponse response,
                                  @RequestParam String maps) throws IOException {
        UserDTO authentication = userService.getAuthentication();
        String account = authentication.getAccount();
        String strid = IdGen.strid() + account;
        //做判断
        stringRedisTemplate.opsForValue().set(strid,maps,5000,TimeUnit.MILLISECONDS);
        return Result.success(strid);
    }

    /**
     * 列表导出excel公用方法
     * @param request request
     * @param response response
     * @param type type
     */
    @RequestMapping("/azbj/abwordresource/common/exportExcelFile")
    public void downloadFile(HttpServletRequest request,HttpServletResponse response,
                             @RequestParam String type) throws IOException {
        UserDTO loginUserInfo = userService.getAuthentication();
        String departId = loginUserInfo.getDepartid();
        String account = loginUserInfo.getAccount();
        String level = loginUserInfo.getLevel();
        Map<String,Object> map = new HashMap<>(16);
        List<Map<String,Object>> mapList;
        if(PL_MGX.equals(type)) {
            //搜索条件
            map.put("searchMap",redisTemplate.opsForHash().entries(account));
            uploadExcelService.exportExcelFileTwo(request,map,"工作台信息表",response,PL_MGX);
        }
        if(WORK_WORDER_INFO.equals(type)) {
            String priKey = request.getParameter("priKey");
            //数据和搜索条件
            map.put("dataList",JSONArray.parseObject(stringRedisTemplate.opsForValue().get(priKey),List.class));
            map.put("searchMap",redisTemplate.opsForHash().entries(account));
            uploadExcelService.exportExcelFileTwo(request,map,"工作台信息表",response,WORK_WORDER_INFO);
        }
        if(AB_WORDER_INFO.equals(type)) {
            mapList = staffInfoManageService.selectStaffInfoForExport(departId);
            map.put("dataList",mapList);
            uploadExcelService.exportExcelFile(request,map,"安帮工作人员信息表.xlsx","templates/exceltemp/staffInfo.xlsx",response);
        }
        if(AZBJ_JD_INFO.equals(type)) {
            mapList = placementBaseInfoService.selectPlaceBaseForExport(departId);
            map.put("dataList",mapList);
            uploadExcelService.exportExcelFile(request,map,"安帮基地信息表.xlsx","templates/exceltemp/placementBase.xlsx",response);
        }
        if(JY_PEIXUN_INFO.equals(type)) {
            mapList = socialTrainingService.selectSocialTrainingForExport(departId);
            map.put("dataList",mapList);
            uploadExcelService.exportExcelFile(request,map,"就业培训机构信息表.xlsx","templates/exceltemp/socialTrainingInfo.xlsx",response);
        }
        if(JY_POSITION_INFO.equals(type)) {
            mapList = socialEmploymentService.selectSocialemploymentForExport(departId);
            map.put("dataList",mapList);
            uploadExcelService.exportExcelFile(request,map,"就业岗位信息表.xlsx","templates/exceltemp/socialEmploymentInfo.xlsx",response);
        }
        if(ZF_JZBF_INFO.equals(type)) {
            mapList = governmentHelpService.selectBfInfoForExport(departId);
            map.put("dataList",mapList);
            uploadExcelService.exportExcelFile(request,map,"政府救助帮扶信息表.xlsx","templates/exceltemp/zfjzbfInfo.xlsx",response);
        }
        if(AB_SHZZ_INFO.equals(type)) {
            mapList = socialOrganizeInfoService.selectSocialOrgInfoForExport(departId);
            map.put("dataList",mapList);
            uploadExcelService.exportExcelFile(request,map,"安帮社会组织信息表.xlsx","templates/exceltemp/socialOrgInfo.xlsx",response);
        }
        if(ZYZ_INFO.equals(type)) {
            mapList = volunteerManageService.selectVolunteerInfoForExport(departId);
            map.put("dataList",mapList);
            uploadExcelService.exportExcelFile(request,map,"志愿者信息表.xlsx","templates/exceltemp/volunteerInfo.xlsx",response);
        }
        if(BJ_ZL.equals(type)) {
            Map<String,Object> mapkey = new HashMap<>(16);
            mapkey.put("account",account);
            mapkey.put("level",level.substring(1));
            mapList = azbjDatabaseInfoService.selectAzbjDatabaseInfoForExport(mapkey);
            map.put("dataList",mapList);
            uploadExcelService.exportExcelFile(request,map,"帮教资料信息表.xlsx","templates/exceltemp/azbjDatabaseInfo.xlsx",response);
        }
    }
}